//
//  LHYPDFEditSignAddListView.m
//  DrawLine
//
//  Created by lhy on 2021/3/3.
//

#import "LHYPDFEditSignAddListView.h"
#import "LHYDefine.h"

@interface LHYPDFEditSignAddListView () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *signTableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation LHYPDFEditSignAddListView

+ (instancetype)shareSignAddListView {
    LHYPDFEditSignAddListView *view = [[[NSBundle mainBundle] loadNibNamed:@"LHYPDFEditSignAddListView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(100, kScreenSizeHeight - 700 - kNavBarAndStatusBarHeight, kScreenSizeWidth - 200, 400);
    return view;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.signTableView.delegate = self;
    self.signTableView.dataSource = self;
    // 进入就打开编辑模式
    [self.signTableView setEditing:YES animated:YES];
    
    for (int i = 0; i < 4; i++) {
        [self.dataArr addObject:[NSString stringWithFormat:@"%d", i]];
    }
}

///左上角 +
- (IBAction)actionItemAdd:(UIButton *)sender {
    
    [self.dataArr insertObject:@"新添加" atIndex:0];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.signTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
///右上角 完成
- (IBAction)actionItemOK:(UIButton *)sender {
    [self hide];
}

- (void)show {
    UIWindow *window = [[UIApplication sharedApplication] windows].firstObject;
    [UIView animateWithDuration:0.2 animations:^{
        [window addSubview:self];
        self.center = CGPointMake(window.center.x, window.center.y - 100);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide {
    [self removeFromSuperview];
}

#pragma mark - delegate / dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MineViewController";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.imageView.image = [UIImage imageNamed:@"lmr_cat"];
    cell.textLabel.text = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//// 可以左滑删除
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    return YES;
//}
// 定义编辑样式
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    return UITableViewCellEditingStyleDelete;
//}
// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.dataArr removeObjectAtIndex:indexPath.row];
        // 有动画
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}




- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

@end
